package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.common.result.Result;
import com.system.runtai.entity.*;
import com.system.runtai.service.AppletUserService;
import com.system.runtai.service.PostedCollectService;
import com.system.runtai.service.PostedService;
import com.system.runtai.service.UpvoteService;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 行业社区收藏 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "行业社区收藏")
@RestController
@RequestMapping("/runtai/posted-collect")
public class PostedCollectController {

    @Autowired
    private PostedCollectService postedCollectService;
    @Autowired
    private UpvoteService upvoteService;

    @Autowired
    private PostedService postedService;

    @Autowired
    private AppletUserService appletUserService;




    @ApiOperation(value = "所有行业社区收藏")
    @GetMapping("/postedCollectList")
    public Result<IPage<PostedCollect>> postedCollectList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){
        Page<PostedCollect> recommendPage = new Page<>(page,limit);
        IPage<PostedCollect> iPage = postedCollectService.page(recommendPage);
        return Result.ok(iPage);

    }

    @ApiOperation(value = "我收藏的行业动态")
    @GetMapping("/selectPostedCollect")
    public Result<IPage<Posted>> selectPostedCollect(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit,Posted car) {
        String s = AuthContextUtil.get();
        LambdaQueryWrapper<PostedCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PostedCollect::getAppletUserId, Integer.valueOf(s));
        List<PostedCollect> list = postedCollectService.list(wrapper);
        List<Posted> postedList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Integer postedId = list.get(i).getPostedId();
            Posted byId = postedService.getById(postedId);
            postedList.add(byId);
        }

        for (Posted ipa : postedList) {
            LambdaQueryWrapper<Posted> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.orderByDesc(Posted::getCreateTime).eq(Posted::getId, ipa.getId());
            List<Posted> list1 = postedService.list(wrapper1);
            Page<Posted> recommendPage = new Page<>(page, limit);
            IPage<Posted> iPage = postedService.page(recommendPage, wrapper1);

            for (Posted record : iPage.getRecords()) {
                LambdaQueryWrapper<Upvote> wrapper3 = new LambdaQueryWrapper<>();
                wrapper3.eq(Upvote::getAppletUserId, Integer.valueOf(s)).eq(Upvote::getCommentid, 1).eq(Upvote::getDynamicid, record.getId());
                Upvote one = upvoteService.getOne(wrapper3);

                if (one != null) {
                    record.setLike(1);
                }

                LambdaQueryWrapper<PostedCollect> wrapper2 = new LambdaQueryWrapper<>();
                wrapper2.eq(PostedCollect::getAppletUserId, Integer.valueOf(s)).eq(PostedCollect::getPostedId, record.getId());
                PostedCollect one1 = postedCollectService.getOne(wrapper2);

                if (one1 != null) {
                    record.setCollect(1);
                }

            }

            return Result.ok(iPage);
        }

        LambdaQueryWrapper<PostedCollect> wrapper6 = new LambdaQueryWrapper<>();
        wrapper6.eq(PostedCollect::getAppletUserId, Integer.valueOf(s));
        List<PostedCollect> list6 = postedCollectService.list(wrapper6);
        List<Posted> postedList6 = new ArrayList<>();
        for (int i = 0; i < list6.size(); i++) {
            Integer postedId = list6.get(i).getPostedId();
            Posted byId = postedService.getById(postedId);
            postedList6.add(byId);
        }

        IPage<Posted> iPage = null;
        for (Posted ipa : postedList6) {
            Page<Posted> recommendPage = new Page<>(page, limit);
            LambdaQueryWrapper<Posted> wrapper7 = new LambdaQueryWrapper<>();
            Integer id = car.getId();

            if (id != null) {
                wrapper7.orderByDesc(Posted::getCreateTime).eq(Posted::getId, ipa.getId());
            }
            iPage = postedService.page(recommendPage, wrapper7);

            for (Posted record : iPage.getRecords()) {
                LambdaQueryWrapper<Upvote> wrapper3 = new LambdaQueryWrapper<>();
                wrapper3.eq(Upvote::getAppletUserId, Integer.valueOf(s)).eq(Upvote::getCommentid, 1).eq(Upvote::getDynamicid, record.getId());
                Upvote one = upvoteService.getOne(wrapper3);

                if (one != null) {
                    record.setLike(1);
                }

                LambdaQueryWrapper<PostedCollect> wrapper2 = new LambdaQueryWrapper<>();
                wrapper2.eq(PostedCollect::getAppletUserId, Integer.valueOf(s)).eq(PostedCollect::getPostedId, record.getId());
                PostedCollect one1 = postedCollectService.getOne(wrapper2);

                if (one1 != null) {
                    record.setCollect(1);
                }

            }
        }
            return Result.ok(iPage);

    }
//    @ApiOperation(value = "程序的行业动态收藏详情")
//    @GetMapping("/selectPosted/{id}")
//    public Result selectPosted(@PathVariable Integer id){
//        Posted byId = postedService.getById(id);
//        byId.setCollect(1);
//        return Result.ok(byId);
//    }

}

